/*
 * $Id: TimeFormatStandard.java,v 1.16 2009/12/16 18:19:14 navdeep_mahajan Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.common.util;

/*
 * TO DO In AutoSC 2 today the start and end times being recorded for tasks, 
 * steps and contact times are all recorded using the application server time 
 * rather than using the database.  There are solutions to allow this to happen 
 * technically but they all have impacts either in performance, complexity or coding 
 * time.  At this point the simplest solution that meet all the needs is to ensure the 
 * servers are all using the same time.  It should not hurt anything but may cause
 * some data weirdness.
 * 
 * Apache provides a library to query the current date using Apache Common NTPUDPClient
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import com.ge.healthcare.autosc.common.ASCUtil;

public class TimeFormatStandard {

	public static final String END_USER_DISPLAY_FORMAT = "dd-MMM-yyyy HH:mm:ss a 'GMT'Z";

	public static final String getNowString() {
		Date date = getNow();
		return TimeFormatStandard.getDateFormatStandard().format(date);
	}

	public static final Date getNow() {
		return getNow(0);
	}

	public static final Date getNow(long adjust) {
		return getNow(adjust, TimeFormatStandard.getDateFormatStandard());
	}

	public static final Date getNow(long adjust, SimpleDateFormat dateFormat) {
		return new Date(System.currentTimeMillis() + adjust);
	}

	public static final Date adjust(Date time, long duration) {
		Calendar gc = GregorianCalendar.getInstance();
		gc.setTime(time);
		Date date = new Date(gc.getTimeInMillis() + duration);
		return date;
	}

	public static final Date getNowMillis() {
		return getNowMillis(0);
	}

	public static final Date getNowMillis(long adjust) {
		return getNow(adjust, TimeFormatStandard
				.getDateFormatStandardWithMillis());
	}

	public static final String format(Date date) {
		return TimeFormatStandard.getDateFormatStandard().format(date);
	}

	public static final String formatYYYYMMDD(Date date) {
		return TimeFormatStandard.getDateFormatYYYYMMDD().format(date);
	}

	public static final Date getDate(String mydate) throws ParseException {
		return TimeFormatStandard.getDateFormatStandard().parse(mydate);
	}

	public static final long convertHourToMillis(long hour) {
		return hour * 60 * 60 * 1000;
	}

	private static final SimpleDateFormat getDateFormatYYYYMMDD() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
		return simpleDateFormat;
	}

	private static final SimpleDateFormat getDateFormatStandard() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss");
		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		return simpleDateFormat;
	}

	private static final SimpleDateFormat getDateFormatStandardWithMillis() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss.SSS");
		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		return simpleDateFormat;
	}

	public static final String getDtInEndUserDisplayFormat(Date date) {
		if (ASCUtil.isNullOrEmptyObject(date)){
			return null;
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				END_USER_DISPLAY_FORMAT);
		return simpleDateFormat.format(date);
	}
}
